﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sys.WebRes.Publish.Areas.Admin.Models;
using Sys.WebRes.Data;
using System.IO;
using System.Security.AccessControl;

namespace Sys.WebRes.Publish.Areas.Admin.Controllers
{
    /// <summary>
    /// 编辑文件资源(一个文件资源代表一组文件)
    /// </summary>
    public partial class ResourceController
    {
        /// <summary>
        /// 获取文件资源，或者新建文件资源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditFiles(int? id)
        {
            var model = GetFilesRes<FilesModel>(id, ResType.File);
            return View(model);
        }

        /// <summary>
        /// 保存文件资源
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditFiles(int? id, FilesModel model)
        {
            id = SaveFilesRes <FilesModel>(id, model, SysConfig.UploadFileFolder);
            return View(model);
            //return RedirectToAction("EditFiles", new { id = id });
        }

        /// <summary>
        /// 删除文件资源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult DeleteFiles(int id)
        {
            DeleteFilesRes(id);
            return View(new FilesModel());
        }

        /// <summary>
        /// 获取文件资源，或者新建文件资源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        private ModelT GetFilesRes<ModelT>(int? id, ResType type)
            where ModelT : FilesModel, new()
        {
            var model = new ModelT();
            if (id == null)
            {
                model.ResTypeID = type.ID; // ResType.File.ID;
                model.StatusTypeID = ResStatusType.NotReviewed.ID;
                model.Sections = new List<SecResLinkModel> { new SecResLinkModel() };
                model.Items = new List<FileModel> { new FileModel() };
            }
            else
            {
                using (var db = DB.New)
                {
                    model = GetResourceModel<ModelT>((int)id, db);
                    model.Items = model.Attachments.Select(p => new FileModel {
                        ID = p.AttachmentID,
                        Title = p.Title,
                        Summary = p.Explain,
                        OrderNo = p.OrderNo,
                        AttachUrlTypeID = p.ResAttachLinkTypeID,
                        Url = p.Url
                    }).ToList();
                }
            }

            return model;
        }

        /// <summary>
        /// 保存文件资源
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        private int SaveFilesRes<ModelT>(int? id, ModelT model, string folder)
            where ModelT : FilesModel, new()
        {
            var attachList = model.Items.Cast<AttachmentModel>().ToList();

            using (var tran = DB.NewTran)
            {
                using (var db = DB.New)
                {
                    AttachController attach = new AttachController();
                    var attachments = attach.UploadFiles(attachList, Request.Files, folder, db);
                    model.Items = attachList.Cast<FileModel>().ToList();
                    model.Attachments = model.Items
                                            .Select(p => new ResAttachLinkModel { 
                                                AttachmentID = p.ID,
                                                Attachment = attachments.FirstOrDefault(a=>a.ID == p.ID),
                                                Title = p.Title,
                                                Explain = p.Summary,
                                                OrderNo = p.OrderNo,
                                                Url = p.Url,
                                                ResAttachLinkTypeID = ResAttachLinkType.Content.ID})
                                            .ToList();

                    SaveResource(db, id, model);
                    db.SaveChanges();
                }
                tran.Complete();
            }

            return model.ID;
        }

        /// <summary>
        /// 删除文件类资源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool DeleteFilesRes(int id)
        {
            using (var tran = DB.NewTran)
            {
                using (var db = DB.New)
                {
                    DeleteLinkResource(db, id);
                    DeleteResourceInfo(db, id, true);

                    db.SaveChanges();
                }
                tran.Complete();
            }

            return true;
        }

        /// <summary>
        /// 删除关联资源
        /// </summary>
        /// <param name="db"></param>
        /// <param name="id"></param>
        private void DeleteLinkResource(ResourceSysEntities db, int id)
        {
            //throw new NotImplementedException();
        }
    }
}